package com.mall.action;

import com.mall.bean.Member;
import com.mall.service.MemberServic;
import com.mall.uitl.JsonUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lwd1513
 * @date 2022-04-25 11 38
 * discription
 */
@RequestMapping("/Index/MemberController2")
@RestController
public class MemberController2 {

    @Autowired
    private MemberServic memberServic;

    //MemberController2 MemberLogin
    @RequestMapping("/MemberLogin")
    public JsonUtil MemberLogin(
            @RequestParam("username") String username,
            @RequestParam("password") String password
    ) {

        Member member = new Member();

        member.setPassword(password);
        member.setUsername(username);

        JsonUtil jsonUtil = new JsonUtil();

        try {
            Member mLogin = memberServic.existUserName(username);
            if (mLogin == null) {
                jsonUtil.setCode(1);
                System.out.println("用户名不存在");
                jsonUtil.setMsg("该用户名不存在，请重新输入或者注册新用户");
                return jsonUtil;
            }

            Member mLoginMember = memberServic.queryMemberByPassword(member);

            if (mLoginMember == null) {
                jsonUtil.setCode(1);
                System.out.println("密码不正确");
                jsonUtil.setMsg("密码不正确，请重新输入");
                return jsonUtil;
            }

            member.setPassword("");
            //System.out.println("对用户做JWT加密后的结果为:" + jsonJwt);
            jsonUtil.setCode(0);
            jsonUtil.setMsg("登录成功");
            jsonUtil.setData(mLoginMember);
            return jsonUtil;

        } catch (Exception e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("登录失败，系统错误");
        }

        return null;
    }

    //getMemberInfoById
    @RequestMapping("/getMemberInfoById/{memberId}")
    public JsonUtil getMemberInfoById(@PathVariable("memberId") String memberId) {
        Member member = memberServic.getMemberInfoById(memberId);
        JsonUtil jsonUtil = new JsonUtil();
        if (member != null) {
            jsonUtil.setData(member);
            jsonUtil.setMsg("成功");
        } else {
            jsonUtil.setCode(1);
            jsonUtil.setMsg("该会员已经不可用，请联系管理员");
        }
        return jsonUtil;
    }

    //registerNewMember
    @RequestMapping("registerNewMember")
    public JsonUtil registerNewMember(Member member) {

        Member existUserName = memberServic.existUserName(member.getUsername());

        JsonUtil jsonUtil = new JsonUtil();
        if (existUserName == null) {
            System.out.println("映射后member对象的值 = " + member);
            int res = memberServic.insert(member);
            jsonUtil.setMsg("成功");
        } else {
            //说明该账号存在 不能注册
            jsonUtil.setCode(1);
            jsonUtil.setMsg("该账号已经存在，请重新输入账号信息");
        }

        return jsonUtil;
    }
}
